package com.dmall.promotion.mapper;

import com.dmall.promotion.domain.User;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户操作Mapper对象
 * @author xiao1.wang
 * @date 2021-06-03 21:46:29
 */
@Repository
public class UserMapper {

    private final ConcurrentHashMap<Integer, User> userMap = new ConcurrentHashMap<>();

    public void insert(User user) {
        userMap.put(user.getId(), user);
    }

    public User select(Integer id) {
        return userMap.get(id);
    }

    public void delete(Integer id) {
        userMap.remove(id);
    }

    public List<User> selectAll() {
        List<User> userList = new ArrayList<>(userMap.size());
        for (Map.Entry<Integer, User> entry : userMap.entrySet()) {
            userList.add(entry.getValue());
        }
        userList.sort(Comparator.comparing(User::getId));
        return userList;
    }
}
